/*********************
考核汇总
* */
<template>
  <basic-container>
    <el-row>
      <targetExaminHead :sign="{ a: true, b: false, c: false,d: false,e: false}" />
    </el-row>
    <el-row style="margin:25px 0 0 0 ;">
      <div :height="searchheight">
        <span style="float:right;margin-top: -30px;cursor: pointer;" @click="searchchangeadvanced">
          {{ searchadvanced ? "查询条件收起" : "查询条件展开" }}
          <i
            :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
          />
        </span>
        <div v-show="searchadvanced">
          <el-form :inline="true" :model="searchForm" class="form-inline">
            <el-form-item label="考核对象">
              <el-input v-model="searchForm.assessRealName" placeholder="考核对象" :clearable="true"></el-input>
            </el-form-item>
            <el-form-item label="考核周期">
              <selectSearch @getDataByPerid="getDataByPerid" ref="selectRef"></selectSearch>
            </el-form-item>
            <el-form-item>
              <el-button
                v-if="permissions.rckhsjkh_view"
                icon="el-icon-search"
                type="primary"
                class="marginLeft"
                @click="getDataList(1)"
              >检索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-row>
    <el-row>
      <div style="border-bottom: 1px solid #b0bde2">

      </div>
      <div style="margin:15px 0 0 0 ">
        <span>指标考核&nbsp;&nbsp;</span>
        <el-radio-group v-model="choosePerOrDept" @change="choosePerOrDeptChange">
          <el-radio :label="0">人员</el-radio>
          <el-radio :label="1">机构</el-radio>
        </el-radio-group>

        <div class="pull-right">
          <el-form :inline="true" :model="getForm" class="form-inline" style="margin: 0px 0;">
            <el-form-item label="计算公式"  prop="normGs">
              <el-select
                style="width: 90%"
                :clearable="true"
                v-model="getForm.normGs"
                placeholder="请选择计算公式"

              >
                <el-option
                  v-for="item in formulaOptions1"
                  :key="item.id"
                  :label="item.gsMc"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="考核周期">
              <el-select
                v-model="getForm.assessId"
                placeholder="请选择周期"
                class="xiaLa"
                style="width: 100%"
                :clearable="true"
                @change="selectBlur"
              >
                <el-option
                  v-for="(item, index) in kezqsList"
                  :key="index"
                  :label="item.name"
                  :value="String(item.value)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button
                plain
                type="primary"
                @click="getSz"
              >获取数据</el-button>
              <el-button
                plain
                type="primary"
                @click="getZdf"
              >生成总得分</el-button>

              <el-button
                plain
                type="primary"
                @click="download"
              >导出</el-button>
            </el-form-item>

          </el-form>
        </div>
      </div>
    </el-row>
    <el-row>
      <el-table
        style="width: 100%;"
        :data="dataList"
        border
        stripe
        header-align="center"
        v-if="rednermsg"
        v-loading="dataListLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column type="index" header-align="center" align="center" label="序号" width="55"></el-table-column>
        <el-table-column prop="assessUserName" align="center" label="编号" show-overflow-tooltip v-if="!choosePerOrDept"></el-table-column>
        <el-table-column prop="assessRealName" align="center" label="考核对象" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="!choosePerOrDept">{{scope.row.assessRealName}}</span>
            <span v-else>{{scope.row.assessDeptName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="assessDeptName" align="center" label="部门" show-overflow-tooltip  v-if="!choosePerOrDept"></el-table-column>
        <el-table-column prop="targetScore" align="center" label="指标得分" show-overflow-tooltip></el-table-column>
        <el-table-column prop="eventScore" align="center" label="事件得分" show-overflow-tooltip></el-table-column>

        <el-table-column prop="eventChangeScore" align="center" label="事件加减分值" show-overflow-tooltip></el-table-column>
        <el-table-column prop="sumScore" align="center" label="总得分" show-overflow-tooltip></el-table-column>
        <el-table-column prop="period" align="center" label="考核周期" show-overflow-tooltip></el-table-column>

      </el-table>
      <!--分页-->
      <div class="avue-crud__pagination">
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageData.pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageData.pageSize"
          :total="pageData.totalPage"
          background
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </div>
    </el-row>

  </basic-container>
</template>

<script>
import targetExaminHead from "@/views/dailyExamine/components/targetExaminHead"; //转态头
import selectSearch from "@/views/dailyExamine/components/select-search.vue"; //流程组件

import {
  gatherList,
  computeScore,
  checkScoreLast,
  defaultList,
  savejghz,
  getLastPeriod,
  gatherExportExcel,examinGatherExportExcel} from "@/api/dailyExamine/targetExamine/khGather"; //接口api
import { getFormula2 } from "@/api/dailyExamine/targetExamine/createAssessment";
import { getKhzqSelect } from "@/api/dailyExamine/eventExamine/event.js";
import { mapGetters, mapState } from "vuex";
import store from "@/store";
export default {
  data() {
    return {
      headers: {
          Authorization: "Bearer " + store.getters.access_token
      },
      deptViewVisible: false,
      personViewVisible: false,
      palnSubVisible: false,
      jsonData: [],
      ids: [],
      usercomment: false,
      choosePerOrDept: 0, //选择的是人员还是机构
      deptVisible: false, //机构考核组件显隐
      personVisible: false, //人员考核组件显隐
      searchheight: "0", //搜索组件的高度
      searchadvanced: true, //搜索的显隐
      searchForm: {}, //搜索对象
      dataListLoading: false, //表格是否加载
      dataList: [], //表格数组
      multipleSelection: [], //勾选数据

      pageData: {
        //分页对象
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0
      },
      headData: {} ,//总部启动的考核数据对象
      rednermsg:true,
      khzqFlag:true,
      getForm:[],
      formulaOptions1: [],
      kezqsList: [],
      createId:"",
    };
  },
  computed: {
      ...mapGetters(["permissions"])

  },
  components: {

    targetExaminHead, //状态头

    selectSearch
  },
  created() {
    this.getDataList();
    this.getHeadList();
    this.getFormulaList();

  },
  methods: {

    //获取数据
    getSz(){
        if(this.getForm.assessId != null && this.getForm.assessId != ""){
            savejghz(this.choosePerOrDept,this.getForm.assessId)
                .then(t => {
                    if(t.data.code == 0){
                        this.$message.success(t.data.msg);
                        this.getDataList();
                    }else{
                        this.$message.error(t.data.msg);
                        this.getDataList();
                    }


                })
                .catch(err => {});
        }else{
            this.$message.error("请选取考核周期");
        }

    },
    getZdf(){
        // 判断总得分
        if(this.getForm.assessId != null && this.getForm.assessId != "" && this.getForm.normGs != null &&  this.getForm.normGs != "") {
            checkScoreLast(this.choosePerOrDept, this.getForm.assessId, this.getForm.normGs).then(res => {
                if (!res.data.data) {
                    this.$confirm("确定要重新生成考核周期的总得分吗?", "提示", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    })
                        .then(result => {
                            //生产总得分
                            computeScore(this.choosePerOrDept, this.getForm.assessId, this.getForm.normGs)
                                .then(t => {

                                    if (t.data.code == 0) {
                                        this.$message.success("操作成功");
                                        this.getDataList();
                                    } else {
                                        this.$message.error(r.data.msg);
                                        this.getDataList();
                                    }


                                })
                                .catch(err => {
                                });

                        })
                        .catch(_ => {

                        });
                } else {
                    //生产总得分
                    computeScore(this.choosePerOrDept, this.getForm.assessId, this.getForm.normGs)
                        .then(t => {
                            if (t.data.code == 0) {
                                this.$message.success("操作成功");
                                //this.dataList = t.data.data;
                                this.getDataList();
                            } else {
                                this.$message.error(r.data.msg);
                                this.getDataList();
                            }


                        })
                        .catch(err => {
                        });
                }
            });
        }else{
            this.$message.error("请选取考核周期和公式");
        }

    },
      getFormulaList(){
          getFormula2().then(res => {
            this.formulaOptions1 = res.data.data.records.filter(it => {
                return it.gsYz == 1;
            });
        });
    },
    selectBlur(e) {
        //this.getForm.period = e.target.value
        //选择的时候强制刷新
        this.$forceUpdate()

    },
    //  人员or机构选择改变
    choosePerOrDeptChange(val) {
      this.searchForm = {};
      this.dataList = [];
      this.$refs.selectRef.resetVal() ; //清空组件的值
      this.pageData.pageIndex = 1 ;
      this.getDataList();
    },

    // 查询总部启动的考核
    getHeadList() {
     /* defaultList()*/
        getLastPeriod()
        .then(r => {

          if (r.data.code == 0) {
              getKhzqSelect().then(res => {
                  res.data.data.map(it => {
                      let item = {
                          name:it.period,
                          value:it.id
                      }

                      this.kezqsList.push(item)
                  });
                  this.getForm.assessId = r.data.data.id;


              });

          } else {
            this.headData = {};
          }
        })
        .catch(err => {
            this.khzqFlag = false;
        });
    },
    // download(){
    //     if(this.getForm.assessId != null && this.getForm.assessId != "") {
    //         var filename = "考核汇总导出" + ".xlsx";
    //
    //         gatherExportExcel(
    //             Object.assign(
    //               {
    //                   type: this.choosePerOrDept,
    //                   assessId: this.getForm.assessId
    //
    //               }
    //             )
    //         ).then((response) => {
    //             this.$message.closeAll();
    //             this.$message.success("导出成功!");
    //             let url = window.URL.createObjectURL(new Blob([response.data]));
    //             let link = document.createElement("a");
    //             link.style.display = "none";
    //             link.href = url;
    //             link.setAttribute("download", filename);
    //             document.body.appendChild(link);
    //             link.click();
    //         });
    //     }else{
    //         this.$message.error("请选取考核周期");
    //     }
    // },
      //导出
    download(){
        let data = this.multipleSelection;
        console.log("data",data)
        if (data.length == 0) {
            this.$message.closeAll();
            this.$message.warning("请选择一个工作！");
            return;
        }
        let filename = "考核汇总数据" + ".xlsx";
        let ids = [];
        for (let i = 0; i < data.length; i++) {
            ids.push(data[i].id);
        }

        examinGatherExportExcel({
            ids: ids,
            type: this.choosePerOrDept
        }).then(response => {
            this.$message.closeAll();
            this.$message.success("导出成功!");
            let url = window.URL.createObjectURL(new Blob([response.data]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            link.setAttribute("download", filename);
            document.body.appendChild(link);
            link.click();
        });
    },
    // 查询条件的展开关闭
    searchchangeadvanced() {
      this.searchadvanced = !this.searchadvanced;
      if (this.searchadvanced) {
        this.searchheight = "auto";
      }
    },
    // 查询列表
    getDataList() {
      ////
      this.dataListLoading = true ;
        gatherList(
        Object.assign(
          {
            current: this.pageData.pageIndex,
            size: this.pageData.pageSize,
            type: this.choosePerOrDept
          },
          this.searchForm
        )
      )
        .then(r => {
          if (r.data.code == 0) {
            this.dataListLoading = false ;
            this.dataList = r.data.data.records.map(it => {
              return it;
            });
            this.pageData.totalPage = r.data.data.total;
          }
        })
        .catch(err => {});
    },
    // 勾选数据
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 条数变化
    sizeChangeHandle(val) {
      this.pageData.pageSize = val;
      this.getDataList();
    },
    // 页码分页变化
    currentChangeHandle(val) {
      this.pageData.pageIndex = val;
      this.getDataList();
    },
    // 关闭人员考核新增弹窗
    closeDialog() {
      this.personVisible = false;
      this.getDataList();
    },
    closeViewDialog() {
      this.personViewVisible = false;
    },

    // 关闭机构考核新增弹窗
    closeDialog1() {
      this.deptVisible = false;
      this.getDataList();
    },

    closeDeptViewDialog() {
      this.deptViewNode = false;
    },

      //选择周期执行搜索功能
      getDataByPerid(period){
          this.searchForm.period = period ;
          this.getDataList() ;
      }
  }
};
</script>

<style lang="scss" scoped>
  .itembadge {
    margin-top: 10px;
    margin-right: 0px;
  }
::v-deep .el-select .el-select__tags {
  overflow: auto !important;
}
::v-deep.el-tag.el-tag--info {
  margin: 5px;
  border: 1px solid green;
}
::v-deep.el-divider {
  background-color: #b0bde2 !important;
}
.weightRed {
  color: rgba(255, 0, 0, 0.726);
}
</style>
